package com.moleq.dialog;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.SmartBeep;
import com.moleq.device.DeviceManager;
import com.moleq.mgpos.activities.R;
import com.moleq.models.ItemDiscountSetting;
import com.moleq.service.ServiceManager;
import com.moleq.util.BigDecmialTool;

public class ItemDiscountDialog extends MoleQBaseDialog {

	private Button mButtonBack, mButtonOk;
	private ListView mListView;
	private List<ItemDiscountSetting> mDataSource = new ArrayList<ItemDiscountSetting>();
	private OpenDiscountAdapter mAdapter;
	private DeviceManager mDeviceManager; // contral device scanner or scale
	private int selectedRow = 0;

	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btnItemDntBack:
				mDeviceManager.enableAllScanner();
				dismiss();
				break;
			case R.id.btn_dialog_ok:

				if(mDataSource.size() <= 0){
					return;	
				}
				onCloseWithReturn(mDataSource.get(selectedRow));
				mDeviceManager.enableAllScanner();
				dismiss();
				
				break;
			case R.id.btn_dialog_cancel:
				mDeviceManager.enableAllScanner();
				dismiss();
				
				break;
			}
		}
	};

	public void onCloseWithReturn(Object obj){
		onReturnValue(0,obj);
		dismiss();
	}
	
	public ListView.OnItemClickListener onItemClickListener = new ListView.OnItemClickListener() {

		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

			switch (parent.getId()) {
			case R.id.lv_dialog_list:
				ScrollToRow(position);
				break;
			}
		}
	};

	public ItemDiscountDialog(Context context) {
		super(context,context);

	}

	public ItemDiscountDialog(Context context, Object base) {
		super(context, base);
	}

	public ItemDiscountDialog(Context context, int theme, Object base) {
		super(context, theme, base);
	}

	public ItemDiscountDialog(Context context, int theme) {
		super(context, theme);
	}


	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set UI layout
		this.setContentView(R.layout.dept_dialog_discount_link);
		this.setCancelable(false);
		mDeviceManager = DeviceManager.getInstance();

		mButtonBack = (Button) findViewById(R.id.btn_dialog_cancel);
		mButtonBack.setOnClickListener(mOnClickListener);

		mButtonOk = (Button) findViewById(R.id.btn_dialog_ok);
		mButtonOk.setOnClickListener(mOnClickListener);

		mListView = (ListView) findViewById(R.id.lv_dialog_list);
		mListView.setOnItemClickListener(onItemClickListener);

		mAdapter = new OpenDiscountAdapter(mContext);
		mListView.setAdapter(mAdapter);

	}

	public void show() {
		super.show();

		mDeviceManager.disableAllScanner();
		mDataSource = ServiceManager.getInstance().mDiscountSettingService.FindAll();
		mAdapter.notifyDataSetChanged();

	}

	class OpenDiscountAdapter extends BaseAdapter {
		private Context maContext;

		public final class ViewHolder {
			public TextView tv_discount_name;
			public TextView tv_discount_value;
		}

		public OpenDiscountAdapter(Context context) {
			maContext = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mDataSource.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mDataSource.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();

				convertView = LayoutInflater.from(maContext).inflate(R.layout.discount_list_item, null);
				holder.tv_discount_name = (TextView) convertView.findViewById(R.id.tv_discount_name);
				holder.tv_discount_value = (TextView) convertView.findViewById(R.id.tv_discount_value);

				convertView.setOnTouchListener(onItemTouchListener);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			String content = mDataSource.get(position).getDs_desc();
			String value = String.valueOf(mDataSource.get(position).getDs_value());
			String type = mDataSource.get(position).getDs_type();
			
			holder.tv_discount_name.setText(content);
			if (type.equals("P")) {
				type = "%";
				holder.tv_discount_value.setText(value+type);
			}else {
				double amount = Double.valueOf(value);
				amount = BigDecmialTool.mul(-1, amount);
				holder.tv_discount_value.setText(String.format("%.2f", amount)+"   ");
			}

			if (position == selectedRow) {
				holder.tv_discount_name.setBackgroundColor(Color.rgb(255, 219, 16));
				holder.tv_discount_value.setBackgroundColor(Color.rgb(255, 219, 16));
			} else {
				holder.tv_discount_name.setBackgroundColor(Color.rgb(204, 204, 204));
				holder.tv_discount_value.setBackgroundColor(Color.rgb(204, 204, 204));
			}
			//
			return convertView;
		}

	}

	private OnTouchListener onItemTouchListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				SmartBeep.getInstance().play(SmartBeep.SOUND_CLICK);
			}
			return false;
		}

	};

	public void ScrollToRow(int row) {
		selectedRow = row;
		mAdapter.notifyDataSetChanged();
	}


}
